﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BizCollegeWebApp.Models;

namespace BizCollegeWebApp.Controllers
{
    public class TestController : Controller
    {
        BizCollegeDBDataContext db = new BizCollegeDBDataContext();
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

        //public ActionResult AnswerQuestion(int id)
        //{
        //    CourseCompletionTest question = this.db.CourseCompletionTests.FirstOrDefault(q => q.QuestionId == id);
        //    return View(question);
        //}

        //[HttpPost]
        //public ActionResult AnswerQuestion(int id)
        //{
        //    CourseCompletionTest question = this.db.CourseCompletionTests.FirstOrDefault(q => q.QuestionId == id);
        //    if (question == null)
        //    {
        //        TempData["message"] = "No question found";
        //        return RedirectToAction("Index");
        //    }

        //    return View(question);

        //}

        //
        // GET: /Course/CreateTest

        public ActionResult CreateTest()
        {
            CourseCompletionTest test = new CourseCompletionTest();
            return View(test);
        }

        //
        // POST: /Course/CreateTest

        [HttpPost]
        public ActionResult CreateTest(CourseCompletionTest test)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //test. = 0;
                    //test.CompletedNumber = 0;
                    //test.AvgTimeSpendInMins = 0;

                    //test.CourseStateID = this.db.GetUnderReviewCourseState();

                    this.db.CourseCompletionTests.InsertOnSubmit(test);
                    this.db.SubmitChanges();

                    return RedirectToAction("TestIndex", new { TestId = test.TestId });
                }
                return View(test);
            }
            catch
            {
                return View(test);
            }
        }


        [HttpGet]
        public ActionResult TestIndex()
        {
            return View(this.db.CourseCompletionTests.ToList());
        }

        [HttpGet]
        public ActionResult TakeTest(int id)
        {
            return View(this.db.CourseCompletionTests.Where(c => c.TestId == id));
        }


        [HttpPost]
        public ActionResult TakeTest(CourseCompletionTest test, Course course, CourseCompletionTest question)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    //test. = 0;
                    //test.CompletedNumber = 0;
                    //test.AvgTimeSpendInMins = 0;

                    //test.CourseStateID = this.db.GetUnderReviewCourseState();


                    //for (int i = 0; i < 3; i++)
                    //{
                    //    int QuestionId = i;
                    return RedirectToAction("TestIndex", new { id = course.CourseID, TestId = test.TestId, QuestionId = question.QuestionId });
                    //this.db.CourseCompletionTests.InsertOnSubmit(test);
                    //  this.db.SubmitChanges();
                    //}
                }
                return View(test);
            }
            catch
            {
                return View(test);
            }
        }

        public ActionResult PreReqTest(int courseId)
        {
            CourseCompletionTest test = this.db.CourseCompletionTests.FirstOrDefault(t => t.IsKnowledge && t.CourseId == courseId);
            return View("Test", test);
        }

        public ActionResult CompletionTest(int courseId)
        {
            CourseCompletionTest test = this.db.CourseCompletionTests.FirstOrDefault(t => t.IsKnowledge == false && t.CourseId == courseId);
            return View("Test", test);
        }


    }
}
